package com.youan.serve.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.youan.pangu.security.bean.LoginInfor;
import com.youan.serve.controller.swagger.SysarguControllerSwagger;
import com.youan.serve.dao.domain.SysArgu;
import com.youan.serve.service.ISysarguService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author auto_code
 * @since 2020-03-04
 */
@Controller
@RequestMapping("/sysargu")
public class SysArguController implements SysarguControllerSwagger{
    @Autowired
    private ISysarguService sysarguService;

    @Override
    @PostMapping("/sysargu")
    public SysArgu addSysargu(SysArgu sysargu, LoginInfor loginInfor) throws Exception{
        if(sysarguService.judgeArgu(sysargu)){
            sysargu.setArguId(UUID.randomUUID().toString().replaceAll("-", ""));
            sysargu.setOperator(loginInfor.getUsername());
            sysargu.setOperatedTime(new Date());
            sysarguService.save(sysargu);
        }
        return null;
    }

    @Override
    @PutMapping("/sysargu")
    public SysArgu updateSysargu(SysArgu sysargu, LoginInfor loginInfor) throws Exception{
        if(sysarguService.judgeArgu(sysargu)){
            sysargu.setOperator(loginInfor.getUsername());
            sysargu.setOperatedTime(new Date());
            sysarguService.updateById(sysargu);
        }
        return null;
    }

    @Override
    @GetMapping("/sysargus")
    public List<SysArgu> selectAllSysargu(){
        return sysarguService.list();
    }

    @Override
    @DeleteMapping("/sysargu/{arguId}")
    public void deleteSysarguByArguId(@PathVariable String arguId){
        sysarguService.removeById(arguId);
    }

    @Override
    @GetMapping("/sysargu/{arguId}")
    public SysArgu selectSysarguByArguId(@PathVariable String arguId){
        return sysarguService.getById(arguId);
    }

    @Override
    @GetMapping("/sysargu/conditions")
    public List<SysArgu> getSysArgusByConditions(String arguName) throws Exception{
        if(arguName==null || arguName.equals("")){
            return sysarguService.list();
        }
        return sysarguService.list(new LambdaQueryWrapper<SysArgu>().likeRight(SysArgu::getArguName, arguName));
    }
}

